package wiiusej.motionAlgorithms;

import java.text.SimpleDateFormat;
import java.util.Vector;

import wiiusej.apptest.statistics.AccelerationPaquet;

import com.sun.xml.internal.ws.api.message.Packet;






public class PacketParametersCalculator {

	
	
	public PacketParametersCalculator(){
		
		
	}
	

	public void calculeMotionParameters(Vector<AccelerationPaquet> myEventVector) {
		
		SimpleDateFormat tsFormat = new SimpleDateFormat("mm:ss:SS");
		
		for(AccelerationPaquet accelerationPacket : myEventVector){
			
			double packetAverage = accelerationPacket.getCounter();
			
			//compute averages 
			double xMean = computeAverage(accelerationPacket.getAccX());
			double yMean = computeAverage(accelerationPacket.getAccY());
			double zMean = computeAverage(accelerationPacket.getAccZ());
			
			accelerationPacket.setMeans(xMean, yMean, zMean);
			
			double xVar = computeVar(xMean, accelerationPacket.getAccX());
			double yVar = computeVar(yMean, accelerationPacket.getAccY());
			double zVar = computeVar(zMean, accelerationPacket.getAccZ());
			
			accelerationPacket.setVariances(xVar, yVar, zVar);			
			
		}
		
		
	}


	private double computeVar(double mean, Vector<Integer> AccVector) {
		
		double deviation, variance = 0;
		
		for(Integer i : AccVector){
			deviation = i - mean;
			variance += Math.pow(deviation,2);			
		}
				
		return variance/AccVector.size();
	}


	private double computeAverage(Vector<Integer> AccVector) {
		double sum = 0;
		for(Integer i : AccVector){
			sum += i;			
		}
		
		return sum/AccVector.size();
				
	}

}
